package com.panshi.controller;

import com.panshi.service.*;
import com.panshi.vo.CountVo;
import com.panshi.vo.InstationVO;
import com.panshi.vo.PageSizeVO;
import com.panshi.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private AnswerService answerService;

    @Autowired
    private ArticlrService articlrService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private CollectionService collectionService;

    @GetMapping("/user/count/{uid}")
    @ResponseBody
    public CountVo queryallcount(@PathVariable int uid){
        CountVo countVo=new CountVo();
        countVo.setAnsercount(answerService.queryanswercount(uid)+answerService.queryanswernocount(uid));
        countVo.setArticlecount(articlrService.queryarticlecount(uid)+articlrService.queryarticlenocount(uid));
        countVo.setQuestioncount(questionService.queryquestioncount(uid)+questionService.queryquestionnocount(uid));
        countVo.setCollectioncount(collectionService.querycollectioncount(uid));
        return countVo;
    }



    @GetMapping("/user/{uid}")
    @ResponseBody
    public UserVO findUserQuery(@PathVariable int uid)  {
        UserVO userQuery = userService.findUserQuery(uid);
       userQuery.setCollectCount(dynamicService.queryusercount(uid));
        System.out.println("查询了用户===========================================");
        System.out.println(userQuery);
        return userQuery;
    };

    @PostMapping("/user/upda")
    @ResponseBody
    public int updateUser(@RequestBody UserVO userVO){
        log.info("修改用户");
        userService.updateUser(userVO);
        return 0;
    };


    @PostMapping("/user/uppo")
    @ResponseBody
    String judge (@RequestBody UserVO userVO){
        UserVO userQuery = userService.findUserQuery(userVO.getUid());
        if(userQuery.getPassword()!=userVO.getPwdOld()){
            return "no";
        }
        userService.upDatePossword(userVO);
        return "ok";
    }


    @PostMapping("/insQu")
    @ResponseBody
    public List<InstationVO> findInsQuery(@RequestBody PageSizeVO pageSizeVO){
        return userService.findInsQuery(pageSizeVO);
    }

    @PostMapping("/user/uuu")
    @ResponseBody
    public PageSizeVO<List<UserVO>> findUserQueryList(@RequestBody PageSizeVO pageSizeVO){
        log.info("11111");
        PageSizeVO<List<UserVO>> ps=new  PageSizeVO();
        ps.setData(userService.findUserQueryList(pageSizeVO));
        ps.setCount(userService.findCount());
        return ps;
    }

}
